/*  main variable and function used to select&replace avatarImg */
var avatarData = "[avatar:10]";
var cmtList = document.getElementById("comments");
var cmtForm = document.forms[0];
var cmtTextarea = cmtForm.content;

if(cmtList) {
	cmtList.innerHTML = cmtList.innerHTML.replace(/\[avatar:(\d{2})\]/g,'<img class="userAvatar" src="http://files.blogbus.com/mobanxx.blogbus.com/files/120011712$1.bmp">');
}
cmtForm.onsubmit = replaced;

document.writeln(" <div id=\"avatarSelection\">");
document.writeln(" 	<select name=\"selectYourAvatar\" onchange=\"changeAvatar(this.options[this.options.selectedIndex].value)\"> ");
document.writeln(" 		<option value=\"10\">Alien1</option> ");
document.writeln(" 		<option value=\"11\">Alien2</option> ");
document.writeln(" 		<option value=\"12\">Ballon</option> ");
document.writeln(" 		<option value=\"13\">Bear</option> ");
document.writeln(" 		<option value=\"14\">Beaver</option> ");
document.writeln(" 		<option value=\"15\">Birthday Cake</option>");
document.writeln(" 		<option value=\"16\">Chocolate Cake</option>");
document.writeln(" 		<option value=\"17\">Da Vinci</option> ");
document.writeln(" 		<option value=\"18\">Dragon</option>");
document.writeln(" 	</select>");
document.writeln(" 	<img id=\"avatar\" src=\"http://files.blogbus.com/mobanxx.blogbus.com/files/12001171210.bmp\" />");
document.writeln(" </div>");

function changeAvatar(value) {
	var avatarImg = document.getElementById("avatar");
	avatarImg.src = 'http://files.blogbus.com/mobanxx.blogbus.com/files/120011712'+value+'.bmp';
	avatarData = '[avatar:'+value+']';
}
function replaced() {
	if(check_cmt(cmtForm))  { 
		cmtTextarea.value = avatarData+cmtTextarea.value; 
	}
   else { return false; }
}

/*insert avatarSelectDiv above h2 */
var commentForm = document.getElementById("commentForm");
var cmtH2 = commentForm.getElementsByTagName("h2")[0];

var avatarDiv = document.createElement("div");
avatarDiv.setAttribute("id","avatarSelectDiv");

cmtH2.appendChild(avatarDiv);
document.getElementById("avatarSelectDiv").innerHTML = document.getElementById("avatarSelection").innerHTML;

/*  try to replace sidebar innerHTML */
var cmtList = document.getElementById("nComments");
if(cmtList) {
	cmtList.innerHTML = cmtList.innerHTML.replace(/\[avatar:\d{2}\]/g,"");
}

/* try to remember which avatar you selected */
	var avatarImgSelection = document.getElementsByName("selectYourAvatar");
	var avatarImgSelectionOptions = avatarImgSelection[0].options;
	avatarImgSelection[0].onchange = selectedIndexSave;

	function selectedIndexSave() {
		document.cookie = "avatarImgSelectionIndex="+avatarImgSelectionOptions.selectedIndex+"; user='jiee'";
	//alert(document.cookie);
	changeAvatar(avatarImgSelectionOptions[avatarImgSelectionOptions.selectedIndex].value)
	}

window.onload = function() {
	cookieArray = document.cookie.split(";");
	//alert(cookieArray.length);
	for (var i=0;i<cookieArray.length;i++) {
		if(cookieArray[i].indexOf("avatarImgSelectionIndex")!=-1) {
			avartarImgIndex = cookieArray[i].substring(cookieArray[i].indexOf("=")+1,cookieArray[i].length);
			avatarImgSelectionOptions.selectedIndex = parseInt(avartarImgIndex);
changeAvatar(avatarImgSelectionOptions[avatarImgSelectionOptions.selectedIndex].value)
			//alert(avartarImgIndex);
		}
	}
}